package com.tuyou.springboot.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * servlet是单例的
 */
@WebServlet(urlPatterns = "/com/tuyou/springboot/servlet/hello", initParams = {@WebInitParam(name="name", value="涂有")})
public class ServletTest extends HttpServlet {

    /**
     * init方法只会执行一次
     * @param config
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        System.out.println(getServletConfig().getInitParameter("name"));
        req.setAttribute("name", getServletConfig().getInitParameter("name"));
        System.out.println("111111111111111");

        req.removeAttribute("name");
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}